package com.zbkj.common.utils.sample;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dypnsapi.model.v20170525.GetMobileRequest;
import com.aliyuncs.dypnsapi.model.v20170525.GetMobileResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.gson.Gson;
import com.zbkj.common.utils.CrmebUtil;
import com.zbkj.common.utils.RandomStrUtil;
import com.zbkj.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@Slf4j
public class Sample {


    // 阿里云短信配置信息
    private static final String accessKeyId = "LTAI5tKSTGYtzCAegWEPsfwW";//KeyId
    private static final String accessKeySecret = "3eaep1dqzxG9LIGujCuo29fqIW1dkn";//KeySecret
    private static final String REGION_ID = "cn-hangzhou";//服务地址
    private static final String PRODUCT = "Dysmsapi"; //短信API产品名称（固定值，就填Dysmsapi）
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";//短信API产品的域名（固定值，就填dysmsapi.aliyuncs.com）


    // 签名
    private static final String signName = "阿里云短信测试";



    /**
     *  发送短信通知
     * @param mobile 手机号
     * @param code 验证码
     * @param signName 公司名称
     * @param type 短信模版类型
     * @return
     */
    public static boolean sendSMS(String mobile, String code,String signName,Integer type ) {
        try {
            IClientProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);

            DefaultProfile.addEndpoint(REGION_ID, REGION_ID, PRODUCT, DOMAIN);

            IAcsClient acsClient = new DefaultAcsClient(profile);

            SendSmsRequest request = new SendSmsRequest();

            request.setMethod(MethodType.POST);

            request.setPhoneNumbers(mobile);//手机号

            request.setSignName(signName);//公司名称


            //暂时一块模版
            if (type.equals(1)){
                request.putQueryParameter("TemplateCode", "SMS_236900154");
            }else {
                request.putQueryParameter("TemplateCode", "SMS_236900154");
            }

            request.putQueryParameter("TemplateParam","{\"code\":\""+code+"\"}");

            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

            if ((sendSmsResponse.getCode() != null) && (sendSmsResponse.getCode().equals("OK"))) {
                log.info("发送成功,code:" + sendSmsResponse.getCode());
                return true;
            } else {
                log.info("发送失败,code:" + sendSmsResponse.getCode());
                return false;
            }

        } catch (ClientException e) {
            log.error("发送失败,系统错误！", e);
            return false;
        }
    }

    /**
     * 获取逗号分隔的拼接字符串
     *
     * @param str 用于拼接的字符串集合
     * @return String
     */
    public static String getSplitString(List<String> str) {
        StringBuilder newS = new StringBuilder();
        if (str != null && str.size() > 0) {
            for (String s : str) {
                newS.append(s).append(",");
            }
        }
        if (newS.length() > 0)
            newS.deleteCharAt(newS.length() - 1);// 删除最后一个多余的逗号
        return newS.toString();
    }




    public static void main(String[] args) {

//        List<String> mobiles = new ArrayList<>();
//        mobiles.add("15132772103");
//        String s= getSplitString(mobiles);
//        System.out.println(Sample.sendSMS(s, RandomStrUtil.getRandInt(4)));//手机号和生成验证码

        getPhone("Dfafdafad5422");
    }



    public  static String getPhone(String accessCode) {

        IClientProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);//自己阿里云的配置

        IAcsClient client =new DefaultAcsClient(profile);
        GetMobileRequest request = new GetMobileRequest();
        request.setAccessToken(accessCode);//app端传过来的，需要用户授权拿到
        request.setRegionId("cn-hangzhou");
        String phone = null;
        try {
            GetMobileResponse response = client.getAcsResponse(request);
            if("OK".equals(response.getCode())) {
                phone = response.getGetMobileResultDTO().getMobile();
            };
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return phone;
    }

}
